<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SpecificationAttribute extends Model
{
    use HasFactory;

    protected $fillable = [
        'key',
        'name',
        'type',
        'is_required',
        'sort_order',
        'is_active',
        'options'
    ];

    protected $casts = [
        'is_required' => 'boolean',
        'is_active' => 'boolean',
        'options' => 'array'
    ];

    /**
     * 获取启用的规格属性
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * 按排序获取
     */
    public function scopeOrdered($query)
    {
        return $query->orderBy('sort_order');
    }

    /**
     * 获取必选的规格属性
     */
    public function scopeRequired($query)
    {
        return $query->where('is_required', true);
    }
}